package com.hq.ims.data.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.dao.PlatformTodoStatisticsDetailMapper;
import com.hq.ims.data.entity.PlatformTodoStatisticsDetail;
import com.hq.ims.data.entityenum.EnumPlatformTodoType;
import com.hq.ims.data.vo.PlatformTodoStatisticsDetailMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.ListUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 蒋柏平
 * @detail 平台端待办统计详情 服务实现类
 * @since 20241108
 */
@Service
public class BasePlatformTodoStatisticsDetailService extends BaseService<PlatformTodoStatisticsDetailMapper, PlatformTodoStatisticsDetail> {


    /**
     * 分页
     */
    public List<PlatformTodoStatisticsDetailMapstruct.PlatformTodoStatisticsDetailVo> getByPage(
            RequestPage page,
            PlatformTodoStatisticsDetailMapstruct.PageSearchVo searchVo
    ) {
        //验证
        LambdaQueryChainWrapper<PlatformTodoStatisticsDetail> lq = this.lambdaQuery();
        //排序
        lq.orderByDesc(PlatformTodoStatisticsDetail::getCreateDateTime);
        //查询条件

        //执行分页
        lq.page(page);

        List<PlatformTodoStatisticsDetail> dataList = page.getRecords();
        List<PlatformTodoStatisticsDetailMapstruct.PlatformTodoStatisticsDetailVo> voList = PlatformTodoStatisticsDetailMapstruct.INSTANCE.toPlatformTodoStatisticsDetailVoList(dataList);
        page.setRecords(voList);
        return voList;
    }

    /**
     * 添加,不存在就添加
     */
    @Transactional(rollbackFor = Exception.class)
    public PlatformTodoStatisticsDetail addData(PlatformTodoStatisticsDetailMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdAdd.class);
        PlatformTodoStatisticsDetail entity = this.getByPlatformTodoTypeAndTableId(vo.getPlatformTodoType(), vo.getTableId());
        if (entity != null) {
            return entity;
        }
        //账值新的对象
        entity = new PlatformTodoStatisticsDetail();
        entity.setPlatformTodoStatisticsId(vo.getPlatformTodoStatisticsId());
        entity.setPlatformTodoType(vo.getPlatformTodoType());
        entity.setTableId(vo.getTableId());

        entity.insert();
        return entity;
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void delData(String platformTodoStatisticsDetailId) {
        PlatformTodoStatisticsDetail data = this.getById(platformTodoStatisticsDetailId);
        if (data == null) {
            return;
        }
        data.deleteById();
    }

    /**
     * 获取原始数据详情
     */
    public PlatformTodoStatisticsDetailMapstruct.PlatformTodoStatisticsDetailVo getDetailVo(String platformTodoStatisticsDetailId) {
        PlatformTodoStatisticsDetail entity = this.getById(platformTodoStatisticsDetailId);
        PlatformTodoStatisticsDetailMapstruct.PlatformTodoStatisticsDetailVo vo = PlatformTodoStatisticsDetailMapstruct.INSTANCE.toPlatformTodoStatisticsDetailVo(entity);
        return vo;
    }

    public PlatformTodoStatisticsDetailMapstruct.PlatformTodoStatisticsDetailVo getByPlatformTodoTypeAndTableId(
            EnumPlatformTodoType platformTodoType,
            String dataId
    ) {
        List<PlatformTodoStatisticsDetail> list = this.lambdaQuery()
                .eq(PlatformTodoStatisticsDetail::getPlatformTodoType, platformTodoType)
                .eq(PlatformTodoStatisticsDetail::getTableId, dataId)
                .list();
        if (ListUtils.isExistsEle(list)) {
            return PlatformTodoStatisticsDetailMapstruct.INSTANCE.toPlatformTodoStatisticsDetailVo(list.get(0));
        }
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean isExistsByPlatformTodoTypeAndTableId(
            EnumPlatformTodoType platformTodoType,
            String dataId
    ) {
        Long count = this.lambdaQuery()
                .eq(PlatformTodoStatisticsDetail::getPlatformTodoType, platformTodoType)
                .eq(PlatformTodoStatisticsDetail::getTableId, dataId)
                .count();
        return count > 0;
    }

    /**
     * 根据平台待办类型和表ID删除
     *
     * @param platformTodoType 平台待办类型
     * @param dataId           数据ID
     */
    @Transactional(rollbackFor = Exception.class)
    public void delByPlatformTodoTypeAndTableId(
            EnumPlatformTodoType platformTodoType,
            String dataId
    ) {
        this.lambdaUpdate()
                .eq(PlatformTodoStatisticsDetail::getPlatformTodoType, platformTodoType)
                .eq(PlatformTodoStatisticsDetail::getTableId, dataId)
                .remove();
    }
}